<?php

namespace App\Http\Controllers\User;

use App\Http\Repository\InfoRepository;
use App\Http\Servers\Admin\ImageServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends CommonController
{
    public function index(Request $request, InfoRepository $infoRepository, ImageServer $imageServer)
    {
        if ($request->isMethod('post')) {

            $imageId = \Auth::user()->image;
            if ($request->hasFile('image')) {
                $update['image'] = $imageServer->uploadImage($request->image, 'user', 'user');
            }
            $update['phone'] = $request->phone;
            $update['describe'] = $request->describe;

            $result = \App\User::where('id', \Auth::user()->id)->update($update);
            if ($result) {
                if (!empty($update['image']) && !empty($imageId)) {
                    $imageServer->deleteImage($imageId);
                }
                $message['message'] = message(true, '编辑成功', '用户资料编辑成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '用户资料编辑失败!');
            }
            return back()->with($message);
        }

        $data['infos'] = $infoRepository->getInfoNumByUserGroupChunk(\Auth::user()->id);

        return view('user.index.index', $data);
    }

    //登出后台
    public function logout()
    {
        //注销管理员
        \Auth::logout();
        return redirect('login');
    }
}
